#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
int n;
char s[N];
vector<int> pacman, food;
int main() {
  cin >> n >> s;
  for (int i = 0; i < n; ++i) {
    if (s[i] == 'P') pacman.push_back(i);
    if (s[i] == '*') food.push_back(i);
  }
  int lo = 0, hi = 3 * n;
  while (lo < hi) {
    int mid = (lo + hi) / 2;
    int ptr = 0;
    for (int x : pacman) {
      int from = x, to = x;
      while (ptr < (int)food.size()) {
        from = min(from, food[ptr]);
        to = max(to, food[ptr]);
        int need = to - from + min(to - x, x - from);
        if (need > mid) break;
        ptr++;
      }
    }
    if (ptr == (int)food.size()) {
      hi = mid;
    } else {
      lo = mid + 1;
    }
  }
  cout << lo;
}
